import { fileURLToPath, URL } from "node:url"
import { loadEnv, type UserConfig } from "vite"
import type { ImportMetaEnv } from "./types/env"

export default (mode: string): UserConfig => {
  const env = loadEnv(mode, fileURLToPath(new URL("./", import.meta.url))) as unknown as ImportMetaEnv
  return {
    build: {
      // 打包代码不省略空格
      minify: "terser",
      sourcemap: "inline",
      terserOptions: {
        compress: {
          drop_console: "true".includes(env.VITE_DROP_CONSOLE),
          drop_debugger: "true".includes(env.VITE_DROP_DEBUGGER)
        }
      },
      rollupOptions: {
        output: {
          assetFileNames: "[hash].[name].[ext]",
          manualChunks: (id: string) => {
            // 分包处理
            if (id.includes("node_modules")) return "vendor"
          }
        }
      },
      outDir: "dist",
      assetsDir: "bs_static"
    }
  }
}
